/*
 * digital.h
 *
 * Created: 2012-07-24 07:28:21
 *  Author: Antrykot
 */ 


#ifndef DIGITAL_H_
#define DIGITAL_H_

#include "main.h"
#include "utils.h"
#include "sc-k.h"

#ifndef MEDIA_WX
#define INT0_ENABLE	EIMSK |= (1 << INT0);
#define INT0_DISABLE EIMSK &= ~(1 << INT0);
#else
#define INT0_ENABLE	EIMSK |= (1 << INT1);
#define INT0_DISABLE EIMSK &= ~(1 << INT1);

#endif
#ifdef MEDIA_WX
#define INT1_ENABLE	EIMSK |= (1 << INT1);
#define INT1_DISABLE EIMSK &= ~(1 << INT1);
#endif



#define MAX_BITS_LONG	52
#define MAX_BITS_SHORT	26



#define VERSION 0x03

extern u8 xxxtime;

extern u8 modeftk;

extern u8 d_typ;
extern u16 d_nr;
extern u8 d_addr;
extern u16 d_data;
extern u8 d_crc;

extern u8 ds_typ;
extern u8 ds_addr;
extern u16 ds_meas;
extern u16 ds_data;
extern u8 ds_crc;

extern u8 pulse_f;

extern u8 logon;
extern u8 logged;

extern u8 bits;
extern u8 polarity;

extern u8 setbitcnt;

extern u8 ds_alstate;

extern u8 no_send ;

extern u16 trans_f[70];

#ifdef MEDIA_CH4
#define TYPE		2
#elif MEDIA_CON
#define TYPE		3
#elif MEDIA_COS		
#define TYPE		4
#elif MEDIA_CO2
#define TYPE		2
#elif MEDIA_O2
#define TYPE		6
#elif MEDIA_PS
#define TYPE		28
#elif MEDIA_PD
#define TYPE		29
#elif MEDIA_WX
#define TYPE		1
#endif

//#ifndef DOUBLE_F
#define GF1 209
#define DF1 100

//#define GF0 350
#define GF0 320
#define DF0 210

#define MAXTIME	750
#define F0	191	 
#define F1	153
/*
#else		// 2Mhz
#define GF1 400
#define DF1 200

//#define GF0 350
#define GF0 640
#define DF0 401

#define F0	191
#define F1	153

#define MAXTIME	750
#endif
*/



#define RBUFFSIZE	80

extern u8 rbuff[RBUFFSIZE];
extern u16 dbg_buff[RBUFFSIZE];

#define T1START TCCR1B = 1;
#define T1STOP  TCCR1B = 0;

#define T1_ENABLE	TIMSK1 |= (1 << OCIE1A);
#define T1_DISABLE	TIMSK1 &= ~(1 << OCIE1A);

#define INT2_DISABLE	EIMSK &= ~(1 << INT2)

#define T0START	TCCR0B = 2;		// prescale 
#define T0STOP	TCCR0B = 0;		// stop
#define T0_ENABLE	TIMSK0 |= (1 << TOIE0);
#define T0_DISABLE	TIMSK0 &= ~(1 << TOIE0);

//#define T2_START	TCCR2B = 6;		// div256
//#define T2_STOP		TCCR2B = 0;


extern u8 dig_trans_finish;			// 0 none, 1 - recived, 2 - ;
extern u8 allow_internal_comm;
extern u8 internal_tr_delay;
extern u8 internal_spec_trans;

u8 identify(u16 t1value);
void add_mem(u8 val);

void pulse0();
void pulse1();


u8 get_digi_trans();
void get_short_trans();
void setbits(u16 value, u8 bits);

void clear_T0();
extern u8 t0cnt ;

void send(u8 ftype);	// 0 - long, 1 - short

extern u8 kal_ask; 
extern u16 kal_meas;

#define KAL_IDLE		0x00
#define KAL_ASK			0x01
#define KAL_ASKED		0x02
#define KAL_ACC			0x03
#define KAL_DENY		0x04
#define KAL_INSIDE		0x05
#define KAL_FINISHOK	0x06
#define KAL_FINISHERR	0x07
#define KAL_ZERO_L		0xF1
#define KAL_ZERO_H		0xF2
#define KAL_SENS_L		0xF3
#define KAL_SENS_H		0xF4
#define KAL_SERWIS		0xE0


extern u16 leg_date;
extern u8 menu_options;
extern u8 uw_alarms;

extern u16 v_pow;
extern u16 v_aku;
extern u16 v_dc;
extern u16 v_cur;
extern u16 v_temp;


#define AL_OK		0x00
#define AL_1		0x01
#define AL_2		0x02

extern u16 dbg_f_max;
extern u16 dbg_f_min;

extern u16 dbg_1_rms;
extern u16 dbg_0_rms;
extern u8 dbg_1;
extern u8 dbg_0;


#ifdef MEDIA_WX
#define AL1_ON	PORTC |= (1 << 2);
#define AL2_ON	PORTC |= (1 << 3);

#define AL1_OFF	PORTC &= ~(1 << 2);
#define AL2_OFF	PORTC &= ~(1 << 3);

#define IN1		((PINA & (1 << 6))>> 6)
#define IN2		((PINA & (1 << 7))>> 7)

#define OUT1	((PINC & (1 << 2)) >> 2)
#define OUT2	((PINC & (1 << 3)) >> 3)
#endif

#endif /* DIGITAL_H_ */